package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.mapper.ItemParamMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.pojo.ItemParam;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ItemParamMapper itemParamMapper;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    @Override
    public PageResult getItemList(PageResult pageResult) {
        IPage<Item> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        //可能传入的参数
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(pageResult.getQuery()), "title", pageResult.getQuery());
        IPage<Item> iPage = itemMapper.selectPage(page, queryWrapper);
        pageResult.setTotal(iPage.getTotal())
                .setRows(iPage.getRecords());
        return pageResult;
    }

    @Override
    @Transactional
    public void updateItemStatus(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void deleteItemById(Integer id) {
        itemMapper.deleteById(id);
        itemParamMapper.deleteById(id);
        itemDescMapper.deleteById(id);
    }

    /**
     * 三张表入库
     * item
     * item_param
     * item_desc
     * @param itemVO
     */
    @Override
    @Transactional
    public void saveItem(ItemVO itemVO) throws JsonProcessingException {
            //1.入库商品表
            Item item = itemVO.getItem();
            item.setStatus(true);
            //要求入库后返回主键
            //MP如果设定了主键自增则会自动地实现数据回显（将id存回对象当中）
            itemMapper.insert(item);

            //2.入库商品详情
            ItemDesc itemDesc = itemVO.getItemDesc();
            itemDesc.setId(item.getId());
            itemDescMapper.insert(itemDesc);

            //3.入库商品参数
            //POJO当中有String类型的属性，也有数组
            //原因：数组用于接收前端传来的值，String用于入库
            //数组中每一个元素类型都是一个对象，所以也要准备对应的VO类
            ItemParam itemParam = itemVO.getItemParam();
            //对象转JSON，再存入

            itemParam.setDynamicArgs(MAPPER
                    .writeValueAsString(itemParam
                            .getDynamicArray()));
            itemParam.setStaticArgs(MAPPER
                    .writeValueAsString(itemParam
                            .getStaticArray()));
            itemParam.setId(item.getId());
            itemParamMapper.insert(itemParam);

    }
}
